﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_Comp : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public string comp_id;

        public frm_Edit_Comp()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.comp_id = null;
            this.compDate.EditValue = DateTime.Now.Date;
            LoadCustomer();          
        }

        ////////////////////////////////////
        /// Load data to form
        ///////////////////////////////////        
        public bool LoadData()
        {
            bool ret = true;

            if (string.IsNullOrEmpty(comp_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [222_Comp] WHERE CompID='" + comp_id + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.compID.Text = comp_id;
                        this.robotName.Text = dataReader["RobotName"].ToString();
                        this.compEnglish.Text = dataReader["CompEnglish"].ToString();
                        this.compVietnamese.Text = dataReader["CompVietnamese"].ToString();
                        for (int i = 0; i < this.customerID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.customerID.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["CustomerID"].ToString())
                            {
                                this.customerID.SelectedIndex = i;
                            }
                        }
                        this.weightValue.Text = dataReader["Weight"].ToString();
                        this.compSize.Text = dataReader["CompSize"].ToString();
                        this.planName.Text = dataReader["PlanName"].ToString();
                        if (dataReader["CompDate"] != DBNull.Value)
                        {
                            this.compDate.EditValue = DateTime.Parse(dataReader["CompDate"].ToString());
                        }
                        this.serialNo.Text = dataReader["SerialNo"].ToString();
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }

            return ret;
        }

        ////////////////////////////////////////////
        /// Load Currency List
        ////////////////////////////////////////////
        private void LoadCustomer()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CustomerID, FullName FROM [222_Customer] ORDER BY FullName";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["FullName"].ToString();
                    item.Value = dataReader["CustomerID"].ToString();
                    this.customerID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Customer list");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string new_id = null;
            string comp_english = null;
            string comp_vietnamese = null;            
            string customer_id = null;
            string weight = null;
            string comp_size = null;
            string robot_name = null;
            DateTime comp_date = new DateTime();
            string plan_name = null;
            string serial_no = null;
            
            ComboboxItem item = new ComboboxItem();
            
            if (string.IsNullOrEmpty((this.compID.Text)))
            {
                error = "Please enter Comp ID";
            }
            else
            {
                new_id = this.compID.Text;
            }

            if (string.IsNullOrEmpty(error))
            {
                comp_english = this.compEnglish.Text;
                comp_vietnamese = this.compVietnamese.Text;
                if (this.customerID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.customerID.SelectedItem;
                    customer_id = item.Value.ToString();
                }
                if (!string.IsNullOrEmpty(this.weightValue.Text))
                {
                    float w = 0;
                    if (float.TryParse(this.weightValue.Text, out w))
                    {
                        weight = this.weightValue.Text;
                    }
                    else
                    {
                        error = "Invalid weight";
                    }
                }
                comp_size = this.compSize.Text;
                robot_name = this.robotName.Text;
                if (this.compDate.EditValue != null)
                {
                    comp_date = DateTime.Parse(this.compDate.EditValue.ToString());
                }
                plan_name = this.planName.Text;
                serial_no = this.serialNo.Text;
            }

            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [222_Comp] SET CompID='" + new_id + "', CompEnglish='" + comp_english + "',"
                        + " CompVietnamese=N'" + comp_vietnamese + "', CustomerID='" + customer_id + "', Weight=" + weight + ","
                        + " CompSize='" + comp_size + "', RobotName='" + robot_name + "', CompDate=@compdate,"
                        + " PlanName='" + plan_name + "', SerialNo='" + serial_no + "'"
                        + " WHERE CompID='" + comp_id + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@compdate", SqlDbType.DateTime).Value = comp_date.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Comp updated success.");
                    this.Dispose();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update Comp. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
    }
}
